/*
 * Copyright (C) 2009 Teleca Poland Sp. z o.o. <android@teleca.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.howcan.api.impl;

import java.io.UnsupportedEncodingException;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.howcan.api.HowcanGet2Api;
import com.howcan.api.WSError;
import com.howcan.api.util.Caller;
import com.howcan.api.util.pb;

import android.util.Log;

/**
 * Jamendo Get2 API implementation, Apache HTTP Client used for web requests
 * 
 * @author Lukasz Wisniewski
 */
public class HowcanGet2ApiImpl implements HowcanGet2Api {
	
	private static String GET_API = "http://www.haoshe.org/m/phpwind/mclientapi.php?";

	private String doGet(String url) throws WSError{
		return Caller.doGet(url);
	}

	public String getShopLists() throws JSONException, WSError {
		return doGet("http://www.haoshe.org/m/modoer/howcan.php?act=list&catid=1");
//		if (jsonString == null)
	}		

	public String getCategorys() throws JSONException, WSError {
		return doGet("http://www.haoshe.org/m/modoer/data/cachefiles/category.php");
//		return doGet("http://www.haoshe.org/musicbox/modoer/howcan.php?act=list&catid=1");

	}		

	public String getRegions() throws JSONException, WSError {
		return doGet("http://www.haoshe.org/m/modoer/data/cachefiles/area.php");
	}		

	
//	%5B%22cxandy%22%2C%22a14f6ecee905d1f9a20153059638105a%22%5D&sign=6e773095cd7fd54e676c3f6c5d7ee5c6&

	public String userLogin(String username, String password) throws JSONException, WSError {
		try {
		
//		    return new jb((JSONObject)kw.a(ky.a, "user.checkauth", localLinkedHashMap));
//		      localObject2 = (JSONArray)localLinkedHashMap;
		    JSONArray parms = new JSONArray();
		    parms.put(username);
		    parms.put(pb.c(password));
		    
		    String request_str = GET_API + "callback=user.checkauth&params=" + URLEncoder.encode(parms.toString());
	    
			String jsonString = doGet(request_str);
			return jsonString;
		}	catch (NullPointerException e) {
			e.printStackTrace();
			throw new JSONException(e.getLocalizedMessage());
		}
	}


}
